package com.example.filecollectionsystemdemo1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.filecollectionsystemdemo1.common.Result;
import com.example.filecollectionsystemdemo1.entity.Permissions;
import com.example.filecollectionsystemdemo1.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Permissions)表控制层
 *
 * @author makejava
 * @since 2024-10-08 17:03:20
 */
@CrossOrigin
@RestController
@RequestMapping("permissions")
public class PermissionsController {

    @Autowired
    PermissionsService permissionsService;


    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Permissions> permissionsList = permissionsService.list(new QueryWrapper<Permissions>().orderByDesc("permissionsId"));
        return Result.success(permissionsList);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permissions permissions) {
        try {
            permissionsService.save(permissions);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody Permissions permissions) {
        permissionsService.updateById(permissions);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        permissionsService.removeById(id);
        return Result.success();
    }


}


